package edu.mse.camel.client.posterboard.ui.editor;

import java.util.Iterator;

import org.eclipse.draw2d.PositionConstants;
import org.eclipse.gef.ui.actions.ActionRegistry;
import org.eclipse.gef.ui.actions.AlignmentAction;
import org.eclipse.gef.ui.actions.DirectEditAction;
import org.eclipse.gef.ui.actions.MatchHeightAction;
import org.eclipse.gef.ui.actions.MatchWidthAction;
import org.eclipse.gef.ui.actions.SelectionAction;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;

import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.core.actions.AdaptDimension;
import com.cea.papyrus.core.actions.ApplyStereotypeAction;
import com.cea.papyrus.core.actions.CloseDiagramAction;
import com.cea.papyrus.core.actions.OpenDiagramAction;
import com.cea.papyrus.core.actions.delete.DeleteFromDiagramAction;
import com.cea.papyrus.core.actions.delete.DeleteFromModelAction;
import com.cea.papyrus.core.actions.imports.ImportLibraryFromRepositoryAction;
import com.cea.papyrus.core.actions.imports.ImportPackageFromCurrentModelAction;
import com.cea.papyrus.core.actions.imports.ImportPackageFromFileAction;
import com.cea.papyrus.core.actions.imports.ImportPackageFromWorkspaceAction;
import com.cea.papyrus.core.actions.imports.ImportProfilesAsLibraryFromRepositoryAction;
import com.cea.papyrus.core.actions.profile.DisplayDefinitionHistoryAction;
import com.cea.papyrus.core.actions.selectcopypaste.CopyAction;
import com.cea.papyrus.core.actions.selectcopypaste.PasteAction;
import com.cea.papyrus.core.helper.IModelerHelper;
import com.cea.papyrus.core.helper.ModelerHelperRegistry;
import com.cea.papyrus.core.treeeditpart.CreateElementActionWithCommand;

import edu.mse.camel.client.posterboard.ui.actions.DeleteFromPosterboardAction;
import edu.mse.camel.client.posterboard.ui.actions.ZoomToFit;

public class EditorUtil {

	@SuppressWarnings("unchecked")
	public static ActionRegistry createParentRegistry(IWorkbenchPart part) {
		// for each diagram, create "create actions"
		ActionRegistry parentActionRegistry = new ActionRegistry();
		ModelerHelperRegistry factoryRegistry = 
			PapyrusPlugin.getDefault().getFactoryRegistry();
		Iterator<IModelerHelper> it = factoryRegistry.getFactories().iterator();
		while(it.hasNext()) {
			IModelerHelper factory = it.next();
			Iterator<Class> metaclasses = factory.getDefinedMetaClasses().iterator();
			while(metaclasses.hasNext()) {
				Class metaclass = metaclasses.next();
				// if the action does not exist yet, creates a new one and add it to the repository
				if(parentActionRegistry.getAction(
						CreateElementActionWithCommand.getId(metaclass))== null) {
					parentActionRegistry.registerAction(
							new CreateElementActionWithCommand(part, metaclass));
				}
			}
		}
		return parentActionRegistry;
	}
	
	public static void createActions(ActionRegistry registry, PosterBoardEditor editor) {
		registry.registerAction(new ZoomToFit(editor));
		// graphical mangement
		registerSelectionAction(registry, new MatchHeightAction(editor), editor);
		registerSelectionAction(registry, new MatchWidthAction(editor), editor);
		registerSelectionAction(registry, new AdaptDimension(editor), editor);

		// edit actions
		//Darpan
		registerSelectionAction(registry, new DeleteFromPosterboardAction(editor), editor);
		//Darpan
		registerSelectionAction(registry, new DeleteFromDiagramAction(editor), editor);
		registerSelectionAction(registry, new DeleteFromModelAction(editor), editor);
		registerSelectionAction(registry, new PasteAction(editor), editor);
		registerSelectionAction(registry, new CopyAction(editor), editor);
		
		// alignement action
		registerSelectionAction(registry, new AlignmentAction((IWorkbenchPart)editor, PositionConstants.LEFT), editor);
		registerSelectionAction(registry, new AlignmentAction((IWorkbenchPart)editor, PositionConstants.RIGHT), editor);
		registerSelectionAction(registry, new AlignmentAction((IWorkbenchPart)editor, PositionConstants.TOP), editor);
		registerSelectionAction(registry, new AlignmentAction((IWorkbenchPart)editor, PositionConstants.BOTTOM), editor);
		registerSelectionAction(registry, new AlignmentAction((IWorkbenchPart)editor, PositionConstants.CENTER), editor);
		registerSelectionAction(registry, new AlignmentAction((IWorkbenchPart)editor, PositionConstants.MIDDLE), editor);
		
		// diagram mangement
		registerSelectionAction(registry, new CloseDiagramAction(editor), editor);
		registerSelectionAction(registry, new OpenDiagramAction(editor), editor);
		
		// import package actions
		registerSelectionAction(registry, new ImportPackageFromCurrentModelAction(editor), editor);
		registerSelectionAction(registry, new ImportPackageFromFileAction(editor), editor);
		registerSelectionAction(registry, new ImportLibraryFromRepositoryAction(editor), editor);
		registerSelectionAction(registry, new ImportProfilesAsLibraryFromRepositoryAction(editor), editor);
		registerSelectionAction(registry, new ImportPackageFromWorkspaceAction(editor), editor);
		
		// misc
		registerSelectionAction(registry, new ApplyStereotypeAction(editor), editor);
		registerSelectionAction(registry, new DirectEditAction((IWorkbenchPart)editor), editor);
		registerSelectionAction(registry, new DisplayDefinitionHistoryAction((IWorkbenchPart)editor), editor);
	
	}
	
	@SuppressWarnings("unchecked")
	protected static void registerSelectionAction(
			ActionRegistry registry, SelectionAction action, PosterBoardEditor editor) {
		registry.registerAction(action);
		editor.getSelectionActions().add(action.getId());
	}

	public static IEditorPart openEditor(IWorkbenchPage page, IEditorInput editorInput, String editorID)
			throws PartInitException {
		if (page == null) {
			page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		}
		return page.openEditor(editorInput, editorID);
	}
	
	public static void closeEditor(
			final IWorkbenchPage page, final IEditorPart editor, final boolean save) {
		if (page == null) {
			//page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		}
		Display.getDefault().asyncExec(new Runnable() {
		
			@Override
			public void run() {
				PlatformUI.getWorkbench().getActiveWorkbenchWindow()
							.getActivePage().closeEditor(editor, save);
		
			}
		});
		
	}
	
}
